package com.fymxy.question_bank_management.service.serviceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fymxy.question_bank_management.mapper.PaperMapper;
import com.fymxy.question_bank_management.pojo.Paper;
import com.fymxy.question_bank_management.mapper.Paper2proMapper;
import com.fymxy.question_bank_management.mapper.PaperMapper;
import com.fymxy.question_bank_management.mapper.ProblemMapper;
import com.fymxy.question_bank_management.pojo.*;
import com.fymxy.question_bank_management.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class PaperServiceImpl implements PaperService {

    @Autowired
    private PaperMapper paperMapper;

    @Autowired
    ProblemMapper problemMapper;
    @Autowired
    Paper2proMapper paper2proMapper;

    @Override
    public IPage<Paper> selectAllPaper(Page page) {
        return paperMapper.selectAllPaper(page);
    }




    //添加试卷
    @Override
    public int addPaper(Paper record) {
        return paperMapper.insert(record);
    }

    @Override
    public int getTopPaperNo() {
        return paperMapper.getTopPaperNo();
    }

    @Override
    public int autoGeneratePaper(String level, List<String> keywords, Integer paperNo,String name,Integer paperSetterNo) {
        int problemNum = 3;
        int choiceNum = 2;
        int fillNum = 2;
        int calNum = 1;
        //所有符合的试题
        List<Integer> totalProblems = new ArrayList<>();
        //添加选择题
        List<Integer> choiceProblems = new ArrayList<>();
        choiceProblems.addAll(problemMapper.selectByOption(keywords,level,"choice"));
        if(choiceProblems.size() < choiceNum){
            return -3;
        }
        Collections.shuffle(choiceProblems);
        choiceProblems.subList(0,choiceNum);
        totalProblems.addAll(choiceProblems);
        //
        List<Integer> fillProblems = new ArrayList<>();
        fillProblems.addAll(problemMapper.selectByOption(keywords,level,"fill"));
        if(fillProblems.size() < fillNum){
            return -2;
        }
        Collections.shuffle(fillProblems);
        fillProblems.subList(0,fillNum);
        totalProblems.addAll(fillProblems);
        //
        List<Integer> calProblems = new ArrayList<>();
        calProblems.addAll(problemMapper.selectByOption(keywords,level,"cal"));
        if(calProblems.size() < calNum){
            return -1;
        }
        Collections.shuffle(calProblems);
        calProblems.subList(0,calNum);
        totalProblems.addAll(calProblems);

        for(Integer problemNo:totalProblems){
            System.out.println(problemNo);
        }

        Date date = new Date();
        Paper record = new Paper();
        record.setPaperLevel(level);
        record.setPaperName(name);
        record.setPaperProblemNum(20);
        record.setPaperTestScore(100);
        record.setPaperTime(date);
        record.setPaperSetterNo(paperSetterNo);
        this.addPaper(record);


        //更新paper2pro
        for (Integer problemNo:totalProblems){
            Paper2proKey record2 = new Paper2proKey();
            record2.setPaperNo(paperNo);
            record2.setProblemNo(problemNo);
            paper2proMapper.insert(record2);
        }
        return 1;
    }


    @Override
    public Paper selectByPaperNo(Integer paperNo) {
       return this.paperMapper.selectByPrimaryKey(paperNo);
    }
}
